package com.haisen.dataobject.mapper.base;

import com.haisen.core.utils.MyMapper;
import com.haisen.dataobject.dto.ItemCatDto;
import com.haisen.dataobject.dto.ItemCatVO;
import com.haisen.dataobject.dto.ItemDto;
import com.haisen.dataobject.entity.ItemCat;
import com.haisen.dataobject.entity.ItemParamGroup;
import com.haisen.dataobject.entity.ItemParamKey;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ItemCatMapper extends MyMapper<ItemCat> {
    int updateSalefParentIds(String makeSelfAsParentIds);


    @Select("select a.id,a.parent_id as 'parentId',a.name as 'catName',a.is_leaf as 'leaf',b.id as 'groupId',b.group_name as 'groupName',c.param_name as 'paramName' from tb_item_cat a , tb_item_param_group b,tb_item_param_key c where a.id=b.item_cat_id and b.id=c.group_id and a.id=#{0}")
    List<ItemCatVO> selectItemDto(Long catId);

    /**
     * 根据id查group列表
     * @param id
     * @return
     */
    @Select("select id,parent_id as 'parentId',name,up,sort_order as sortOrder,is_leaf as 'leaf' from tb_item_cat where id=#{0} ")
    @Results({
            @Result(property="parmGroupIdList",javaType=List.class,column="id",many=@Many(select="com.haisen.dataobject.mapper.base.ItemParamGroupMapper.selectGroupByCatId"))
    })
     ItemCat selectGroups(Long id);

    /**
     * 不能直接转换dto，应该数据没有对应的字段，没有赋值，字段管理使用后，其他不再显示
     * @return
     */
    @Select("select id ,parent_id as 'parentId',name,up,sort_order as 'sortOrder',is_leaf as 'leaf',param_group_ids as paramGroupIds,param_group_ids as ids   from tb_item_cat ")
    @Results({
            @Result(property="parmGroupIdList",javaType=List.class,column="groupIds",many=@Many(select="com.haisen.dataobject.mapper.base.ItemParamGroupMapper.selectGroupByCatId"))
    })
    List<ItemCatDto> selectCatAndGroups();


    /**
     * 根据种类id查产品参数列表
     * @param id
     * @return
     */
    @Select("select b.id ,b.param_name as 'paramName',b.group_id as groupId,b.group_name as 'groupName' from tb_item_param_group a,tb_item_param_key b " +
            "where a.item_cat_id=#{0} and  a.id=b.group_id;")
    List<ItemParamKey> selectParamKeys(Long id);
}